package com.gitee.sop.order.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.sop.order.pay.bo.PayConfigBo;
import com.gitee.sop.order.pay.entity.PayConfig;
import com.gitee.sop.order.pay.mapper.PayConfigMapper;
import com.gitee.sop.order.pay.service.PayConfigService;
import com.gitee.sop.order.pay.strategy.AliStrategy;
import com.gitee.sop.order.pay.strategy.WxStrategy;
import com.gitee.sop.order.pay.util.ObjectUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 支付配置信息服务实现类
 *
 * @author Admin
 */
@Service
public class PayConfigServiceImpl extends ServiceImpl<PayConfigMapper, PayConfig> implements PayConfigService {

    private Map<String, PayConfigBo> payTemplate = new HashMap<>(6);

    {
        AliStrategy aliStrategy = new AliStrategy();
        WxStrategy wxStrategy = new WxStrategy();
        payTemplate.put(aliStrategy.code(), new PayConfigBo(aliStrategy.code(), "支付宝支付配置", aliStrategy));
        payTemplate.put(wxStrategy.code(), new PayConfigBo(wxStrategy.code(), "微信支付配置", wxStrategy));
    }


    @Override
    public List<Map<String, Object>> getAllPayConfigTemplate() {
        List<Map<String, Object>> mapList = new ArrayList<>(this.payTemplate.size());
        this.payTemplate.forEach((k, v) -> {
            mapList.add(ObjectUtil.toMapEx(v, "payStrategy"));
        });
        return mapList;
    }


    @Override
    public Map<String, Object> getOnePayConfigTemplate(String code) {
        return payTemplate.get(code).getPayStrategy().payConfigTemplate();
    }

    @Override
    public List<PayConfig> queryOnline() {
        LambdaQueryWrapper<PayConfig> wrapper = new QueryWrapper<PayConfig>().lambda();
        wrapper.select(PayConfig::getId, PayConfig::getCode, PayConfig::getName, PayConfig::getDescription);
        wrapper.eq(PayConfig::getStatus, 1);
        wrapper.orderByDesc(PayConfig::getCreatedTime);
        return this.list(wrapper);
    }

    @Override
    public PayConfig getByCodeAndStatus(String code) {
        LambdaQueryWrapper<PayConfig> wrapper = new QueryWrapper<PayConfig>().lambda();
        wrapper.eq(PayConfig::getStatus, 1);
        wrapper.eq(PayConfig::getCode, code);
        return this.getOne(wrapper);
    }


    @Override
    public void updatePayConfig(PayConfig payConfig) {
        Integer status = payConfig.getStatus();
        String code = payConfig.getCode();

        if (Objects.nonNull(status) && status.intValue() == 1) {
            //保证同一种支付方式启用一个
            LambdaUpdateWrapper<PayConfig> wrapper = new UpdateWrapper<PayConfig>().lambda();
            wrapper.eq(PayConfig::getCode, code);
            wrapper.eq(PayConfig::getStatus, 1);
            PayConfig config = new PayConfig();
            config.setStatus(0);
            this.update(config, wrapper);
        }

        this.updateById(payConfig);

    }
}
